package com.suning.sawp.service.impl.airCondition;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.suning.rsf.consumer.ServiceLocator;
import com.suning.rsf.pcss.dto.item.RsfItemInfoResponseDto;
import com.suning.sawp.dto.aircondition.SNItemInfoResponseDTO;
import com.suning.sawp.intf.airCondition.CommodityDetailRsfService;

/**
 * 商品详情RSF调用接口实现
 * 
 * @author 15050519
 *
 */
@Service
public class CommodityDetailRsfServiceImpl implements CommodityDetailRsfService {

    /**
     * 日志
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(CommodityDetailRsfServiceImpl.class);

    /**
     * 接口执行成功码
     */
    private static String SUCCESS = "0";

    private String COMMON = "00";

    private String GENERAL = "01";

    /**
     * 商品RSF接口服务
     */
    com.suning.rsf.pcss.service.ProductService productService = ServiceLocator.getService(
            com.suning.rsf.pcss.service.ProductService.class, null);

    /**
     * 商品商品详情
     */
    @Override
    public SNItemInfoResponseDTO queryProductBD(String partNumber) {
        // 参数不为空
        if (StringUtils.isNotBlank(partNumber)) {
            try {
                // 查询商品详情
                LOGGER.info("queryProductBD RSF for " + partNumber);
                RsfItemInfoResponseDto resp = productService.queryProductBD(partNumber);
                if (null != resp && SUCCESS.equals(resp.getErrCode())) {
                    LOGGER.info("queryProductBD RSF  SUCCESS for " + partNumber);
                    SNItemInfoResponseDTO item = new SNItemInfoResponseDTO();
                    item.setPartNumber(partNumber);
                    item.setCategoryCode(resp.getCategoryCode());
                    item.setItemId(String.valueOf(resp.getItemId()));
                    item.setItemName(resp.getItemName());
                    item.setItemDisplayName(resp.getItemDisplayName());
                    item.setPublished(resp.getPublished());
                    item.setItemType(commodityTypeConvert(String.valueOf(resp.getItemType())));
                    item.setBrandId(resp.getBrandId());
                    return item;
                }
            } catch (Exception e) {
                LOGGER.error("queryProductBD RSF error,", e);
            }
        }
        return null;
    }

    /**
     * 转换通子码类型
     * 
     * @param esbCommodityype
     * @return
     */
    private String commodityTypeConvert(String esbCommodityype) {
        // 20是通码商品
        if ("20".equals(esbCommodityype)) {
            return GENERAL;
        } else {
            return COMMON;
        }
    }

}
